import axios from 'axios';

import { to } from '@/utils';

const baseUrl = UMI_BASE_URL;

const instance = axios.create({
  baseURL: baseUrl,
  timeout: 60 * 1000,
  headers: {
    'content-type': 'application/json',
    // 'content-type': 'application/x-www-form-urlencoded',
    // 'content-type': 'multipart/form-data',
  },
});

// 请求拦截
instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  },
);
// 相应拦截
instance.interceptors.response.use(
  (response) => {
    const { status, data } = response;
    if (status !== 200) {
      return Promise.reject(status);
    }
    if (data.code !== 0) {
      return Promise.reject(data);
    }
    return Promise.resolve(data.data);
  },
  (error) => {
    return Promise.reject(error);
  },
);

export const request = (req) => {
  return to(
    instance({
      method: 'post',
      ...req,
    }),
  );
};
